package palmtop.com.palmtop.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.beardedhen.androidbootstrap.AwesomeTextView;
import com.google.gson.Gson;

import net.grandcentrix.tray.AppPreferences;

import org.greenrobot.eventbus.EventBus;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import palmtop.com.palmtop.R;
import palmtop.com.palmtop.event.CluelistSyncEvent;
import palmtop.com.palmtop.event.QianshouSyncEvent;
import palmtop.com.palmtop.model.Processtask;
import palmtop.com.palmtop.model.Taskgrid;
import palmtop.com.palmtop.util.Config;
import palmtop.com.palmtop.util.FuncUtil;
import palmtop.com.palmtop.util.NetUtil;
import palmtop.com.palmtop.util.ToastUtil;

public class MytaskdetailActivity extends BaseActivity {

    @BindView(R.id.mtitle)
    TextView mtitle;
    @BindView(R.id.taskname)
    AwesomeTextView taskname;
    @BindView(R.id.pname)
    AwesomeTextView pname;
    @BindView(R.id.addtime)
    AwesomeTextView addtime;
    @BindView(R.id.taskaddress)
    AwesomeTextView taskaddress;
    @BindView(R.id.pnote)
    AwesomeTextView pnote;
    //    @BindView(R.id.footBtn)
//    Button footBtn;
    Taskgrid taskgrid;
    Processtask processtask;
    @BindView(R.id.zphoto1)
    ImageView zphoto1;
    @BindView(R.id.zphoto2)
    ImageView zphoto2;
    @BindView(R.id.zphoto3)
    ImageView zphoto3;
    @BindView(R.id.zphoto4)
    ImageView zphoto4;
    @BindView(R.id.lookmapBtn)
    Button lookmapBtn;
    MyApplication app;
    @BindView(R.id.qianshou)
    Button qianshou;
    @BindView(R.id.dotask)
    Button dotask;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mytaskdetail, Config.toptitle, 1);
        MyApplication app = (MyApplication) getApplication();
        app.addActivityList(MytaskdetailActivity.this);
        ButterKnife.bind(this);
        app = (MyApplication) getApplication();
        app.addActivity(MytaskdetailActivity.this);
        initView();

    }

    private void initView() {
        mtitle.setText("任务详情");
        //footBtn.setText("去完成任务");
        taskgrid = (Taskgrid) getIntent().getSerializableExtra("taskgrid");
        if (taskgrid != null) {
            taskname.setText(taskgrid.getTaskname());
            taskaddress.setText(taskgrid.getTaskaddress());
            pname.setText(taskgrid.getAddusername());
            pnote.setText(taskgrid.getTaskcontent());
            addtime.setText(taskgrid.getAdddate());
            if (taskgrid.getPhoto1() != null && !taskgrid.getPhoto1().equals("")) {
                x.image().bind(zphoto1, Config.IMG_URL + taskgrid.getPhoto1());
                FuncUtil.clickShowBigPic(Config.IMG_URL + taskgrid.getPhoto1(), zphoto1, MytaskdetailActivity.this);
            }
            if (taskgrid.getPhoto2() != null && !taskgrid.getPhoto2().equals("")) {
                x.image().bind(zphoto2, Config.IMG_URL + taskgrid.getPhoto2());
                FuncUtil.clickShowBigPic(Config.IMG_URL + taskgrid.getPhoto2(), zphoto2, MytaskdetailActivity.this);
            }
            if (taskgrid.getPhoto3() != null && !taskgrid.getPhoto3().equals("")) {
                x.image().bind(zphoto3, Config.IMG_URL + taskgrid.getPhoto3());
                FuncUtil.clickShowBigPic(Config.IMG_URL + taskgrid.getPhoto3(), zphoto3, MytaskdetailActivity.this);
            }
            if (taskgrid.getPhoto4() != null && !taskgrid.getPhoto4().equals("")) {
                x.image().bind(zphoto4, Config.IMG_URL + taskgrid.getPhoto4());
                FuncUtil.clickShowBigPic(Config.IMG_URL + taskgrid.getPhoto4(), zphoto4, MytaskdetailActivity.this);
            }


            AppPreferences appPreferences = new AppPreferences(MytaskdetailActivity.this);
            String userid = appPreferences.getString("userid", "");

            RequestParams params = new RequestParams(Config.SERVER_URL + "processtasks/processtasks/getListByTaskidAndPuserid");
            params.addBodyParameter("taskid", String.valueOf(taskgrid.getId()));
            params.addBodyParameter("puserid", userid);
            NetUtil.postData(params, new Callback.CommonCallback<String>() {
                @Override
                public void onSuccess(String result) {
                    if (!(result == null || result.equals("\"\""))) {
                        Gson gson = new Gson();
                        /*Type listType = new TypeToken<ArrayList<Processtask>>() {
                        }.getType();*/
                        processtask = gson.fromJson(result, Processtask.class);
                        dotask.setText("查看详情");

                    } else {
                        dotask.setText("去完成任务");

                    }
                }

                @Override
                public void onError(Throwable ex, boolean isOnCallback) {
                }

                @Override
                public void onCancelled(CancelledException cex) {
                }

                @Override
                public void onFinished() {
                }
            });
        }
    }
    //签收
    @OnClick(R.id.qianshou)
    public void onQianshouClicked() {

        if(processtask!=null){
            ToastUtil.show(this,"已签收，不可重复操作");
            return;
        }else{
            if(taskgrid.getQianshou().equals("0")){
                RequestParams params = new RequestParams(Config.SERVER_URL + "taskgridapp/updateQianshou");
                params.addBodyParameter("id", String.valueOf(taskgrid.getId()));
                NetUtil.postData(params, new Callback.CommonCallback<String>() {
                    @Override
                    public void onSuccess(String result) {
                        if (result.equals("success")) {
                            ToastUtil.show(MytaskdetailActivity.this, "签收成功");
                            EventBus.getDefault().post(new QianshouSyncEvent());
                            finish();

                        } else {
                            ToastUtil.show(MytaskdetailActivity.this, "签收失败");
                        }
                    }

                    @Override
                    public void onError(Throwable ex, boolean isOnCallback) {
                    }

                    @Override
                    public void onCancelled(CancelledException cex) {
                    }

                    @Override
                    public void onFinished() {
                    }
                });
            }else{
                ToastUtil.show(this,"已签收，不可重复操作");
                return;
            }
        }
        /*if(dotask.getText().equals("查看详情")){
            ToastUtil.show(this,"已签收，不可重复操作");
            return;
        }else if(dotask.getText().equals("去完成任务")){
            ToastUtil.show(this,"已签收，不可重复操作");
            return;
        }else{

        }*/

    }
    //
    @OnClick(R.id.dotask)
    public void onDotaskClicked() {
        if (processtask != null) {
            Intent i = new Intent();
            i.putExtra("processtask", processtask);
            i.setClass(MytaskdetailActivity.this, LookDetailActivity.class);
            startActivity(i);
        } else {
            Intent i = new Intent();
            i.putExtra("taskgrid", taskgrid);
            i.setClass(MytaskdetailActivity.this, DotaskActivity.class);
            startActivity(i);
        }
    }

   /* @OnClick(R.id.footBtn)
    public void onViewClicked() {


    }*/

    @OnClick(R.id.lookmapBtn)
    public void onlookmapClicked() {
        Intent i = new Intent();
        if (!TextUtils.isEmpty(taskgrid.getLatitude())) {
            i.putExtra("latitude", taskgrid.getLatitude());
        }
        if (!TextUtils.isEmpty(taskgrid.getLongitude())) {
            i.putExtra("longitude", taskgrid.getLongitude());
        }
        i.setClass(this, LookmapActivity.class);
        startActivityForResult(i, 1);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }



}
